/*
 * Copyright (C) 2011 yixun All Rights Reserved.
 * 
 * DateUtil.java
 */
package com.emm.yixun.basic.system.util;

/**
 * [文件名称]<br>
 * DateUtil <br>
 * <br>
 * [文件描述]<br>
 * 日期类型的转换工具类<br>
 * <br>
 * [修改记录]<br>
 * 2011-8-4 ver1.00 创建 ji_qingyang<br>
 * 
 * @author ji_qingyang
 * @version 1.00
 */
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {

	/**
	 * 构造文件隐藏
	 */
	private DateUtil() {

	}

	/**
	 * 日期格式
	 */
	public static final String DATESTYLE_LONG = "yyyy-MM-dd HH:mm:ss";
	public static final String DATE = "yyyy-MM-dd";
	public static final String DATE_1 = "yyyyMMdd";

	/**
	 * 将日期转换成指定格式的字符串
	 * 
	 * @param date
	 *            日期
	 * @param style
	 *            日期格式
	 * @return 指定格式的字符串
	 */
	public static String dateToString(Date date, String style) {
		SimpleDateFormat dFmt = new SimpleDateFormat(style);
		return dFmt.format(date);
	}

	public static String todayDateStr() {
		Date date = Calendar.getInstance().getTime();
		SimpleDateFormat dFmt = new SimpleDateFormat(DATE);
		return dFmt.format(date);
	}

	public static String todayDateStr1() {
		Date date = Calendar.getInstance().getTime();
		SimpleDateFormat dFmt = new SimpleDateFormat(DATE_1);
		return dFmt.format(date);
	}

	/**
	 * 计算两个日期之间相差的天数
	 * 
	 * @param smdate
	 *            较小的时间
	 * @param bdate
	 *            较大的时间
	 * @return 相差天数
	 * @throws ParseException
	 */
	public static int daysBetween(Date smdate, Date bdate) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		smdate = sdf.parse(sdf.format(smdate));
		bdate = sdf.parse(sdf.format(bdate));
		Calendar cal = Calendar.getInstance();
		cal.setTime(smdate);
		long time1 = cal.getTimeInMillis();
		cal.setTime(bdate);
		long time2 = cal.getTimeInMillis();
		long between_days = (time2 - time1) / (1000 * 3600 * 24);

		return Integer.parseInt(String.valueOf(between_days));
	}

	public static void main(String[] args) throws Exception {
		SimpleDateFormat dFmt = new SimpleDateFormat("yyyy-MM-dd");
		Date d = dFmt.parse("2016-07-02");
		System.out.println(daysBetween(d, new Date()));

	}
}